|
|
|
|
|
|
|
|
Although this chapter refers to functions and statements interchangeably, a conceptual difference does exist between using MsgBox as a function and as a statement. By definition, a function returns a value, whereas a statement does not. Also, the syntax for parameters is slightly different; a function's parameters must be enclosed in parentheses. For example, if you want to see the return value from the MsgBox function, you could type the following line in the Immediate window: |
|
|
|
| |
|
|
|
|
Print MsgBox ("Delete record?", vbYesNo + vbExclamation, "Confirm Delete")  |
|
|
|
|
|
|
|
|
|
|
Although the message box is useful, it does have a few limitations: |
|
|
|
|
|
|
|
|
The message box cannot accept text input from the user. It can only display information and handle the selection of a limited number of choices. |
|
|
|
|
|
|
|
|
You can use only one of four predefined icons and one of six predefined command button sets in the message box. You cannot define your own icons or buttons. |
|
|
|
|
|
|
|
|
By design, the message box requires a user to respond to a message before any part of the program can continue. This means that the message box cannot be used to provide continuous status monitoring of the program because no other part of the program can be executing while the message box is waiting for the user's response. |
|
|
|
|
|
|
|
|
The simplest way to create a message box in your program is to use the MsgBox function as if it were a statement, without returning a value. Using the MsgBox function this way, you simply specify the message text that you want to appear in the message box and then call the function. The message box displays an OK button to allow the user to acknowledge the message. |
|
|
|
|
|
|
|
|
MsgBox "Record processing is complete." |
|
|
|
|
|
|
|
|
When you specify the message text, you can use a string constant (a string of text enclosed in quotation marks, as shown in the preceding line) or a string variable, as in this example: |
|
|
|
|
|
|
|
|
Dim sMyText as String
sMyText = "Hello," & vbCrLf & "World"
MsgBox sMyText |
|
|
|
|
|
|
|
|
Note the use of the intrinsic (predefined) constant vbCrLf, representing a carriage return/line feed combination, to make "Hello" and "World" appear on separate lines. |
|
|
|
|
|
|
|
|
The default message box uses the project's title (which can be set in the Project, Properties dialog) for its caption and only has an OK button. You can dress up your messages somewhat by using two optional parameters: the buttons argument and the title argument. The buttons argument is an integer number that can specify the icon to display in the message box, the command button set to display, and which of the command buttons is the default. The title argument is a text string that specifies custom text to be shown in the title bar of the message box. The full syntax of the MsgBox function is as follows: |
|
|
|
|
|
|
|
|
MsgBox(prompt[, buttons] [, title] [, helpfile, context]) |
|
|
|
|
|